{% extends "personal_center/base.html" %}
<!--固定资产付款创建页-->
{% block page_link %}
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }

        tr td {
            height: 37px;
            outline: none;
        }

        th {
            text-align: center;
        }

        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }

        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }

        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div class="headline">
        <div style="width: 15%;height: 100%;line-height: 40px;float: left;padding-left: 1%;">
            <a href="#" style="color: #0b0b0b;">
                <i class="iconfont icon-faqi"></i>
                <span> 新增申请</span>
            </a>
        </div>

        <div style="height: 100%;line-height: 40px;float: right;text-align: right;padding-right: 15px;">
            <button id="add_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 25px;"
                    onclick="save_data()">
                保存
            </button>
        </div>
        {% if revise != 'true' %}
            <div style="width: 15%;height: 100%;line-height: 40px;float: right;text-align: right;padding-right: 15px;">
            <button id="summit_station" type="button"
                    style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;background-color: #1E90FF;color: white;line-height: 25px;"
                    onclick="ExportCar()">
                提交
            </button>
        </div>
        {% endif %}
    </div>
    <hr>

    <div style="width: 100%;height: 95%;overflow: auto;">
        <div id="menu_from"
             style="width: 100%;height: 100%;color: #2C3E50;display: inline-block;vertical-align: top;background: white;">
            <div style="width: 95%;height: auto;display: inline-block;vertical-align: center;margin-left: 2.5%;">
                <div style="width: 100%">
                    <table style="width: 100%;margin-top: 1%;">
                        <tr style="width: 100%;">
                            <td colspan="4" style="height: 40px;text-align: center;border: #cfcfcf solid 0.5px;"><h3>科研经费申请</h3></td>
                        </tr>
                        <tr>
                            <td colspan="4" style="border: #cfcfcf solid 0.5px;width: 95%;margin-left: 2.5%;height: 40px;padding-left: 0.5%;line-height: 40px;background: #F5F5F5;">
                                基本信息
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;text-align: right;height: 37px;line-height: 37px;padding-right: 1%;">
                                申&nbsp;&nbsp;请&nbsp;人
                            </td>
                            <td id="applicat" style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">{{ realname }}</td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                所属科室
                            </td>
                            <td id="company" style="width: 35%;height: 37px;line-height: 37px;padding-left: 1%;border: #cfcfcf solid 0.5px;">{{ department }}</td>
                        </tr>
                        <tr>
                            <td colspan="4" style="width: 95%;margin-left: 2.5%;height: 40px;padding-left: 0.5%;line-height: 40px;background: #F5F5F5;border: #cfcfcf solid 0.5px;">申请详情</td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                申请事由
                            </td>
                            <td id="reason_application" style="width: 35%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;padding-left: 1%;outline: none;" contenteditable="true">{{ obj.pay_reason }}</td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                人员类型
                            </td>
                            <td style="width: 35%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: left;padding-left: 1%;outline: none;">
                                <select name="" id="people_type" style="width: 100%;height: 100%;border: none;outline: none;">
                                    <option value="0" {% if obj.people_type == '0' %} selected {% endif %}>请选择人员类型</option>
                                    <option value="1" {% if obj.people_type == '1' %} selected {% endif %}>临床医技人员</option>
                                    <option value="2" {% if obj.people_type == '2' %} selected {% endif %}>行管人员</option>
                                </select>
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;letter-spacing: 2px;">
                                收&nbsp;款&nbsp;人
                            </td>
                            <td id="payee" style="width: 35%;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;border-bottom: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;padding-left: 1%;outline: none;" contenteditable="true">{{ obj.payee }}</td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                收款账号
                            </td>
                            <td id="account_number" style="width: 35%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: left;padding-left: 1%;outline: none;" contenteditable="true">{{ obj.account_number }}</td>
                        </tr>
                        <tr>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注
                            </td>
                            <td id="rask" colspan="3" style="width: 85%;height: 37px;line-height: 37px;border: #cfcfcf solid 0.5px;outline: none;padding-left: 1%;" contenteditable="true">{{ obj.rask }}</td>
                        </tr>
                    </table>
                    <table style="width: 100%;" id="PaymentDetails">
                        <tr>
                            <td colspan="7" style="width: 100%;height: 40px;padding-left: 0.5%;line-height: 40px;background: #F5F5F5;border-right: #cfcfcf solid 0.5px;border-left: #cfcfcf solid 0.5px;">
                                <span>付款明细</span>
                                <span style="float: right;padding-right: 10px;color: #00a0e9;cursor: pointer;" onclick="add_other()">添加</span>
                            </td>
                        </tr>
                        <tr style="margin-left: 1%;">
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;text-align: center;height: 35px;line-height: 35px;">货物及费用项目名称</td>
                            <td style="width: 20%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;">规格及技术参数</td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;">数量</td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;">单价（元）</td>
                            <td style="width: 10%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;">预算金额（元）</td>
                            <td style="width: 25%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;">备注</td>
                        </tr>
                        {% if pay_detail|length %}
                            {% for item in pay_detail %}
                                <tr style="margin-left: 1%;">
                                    <td class="goods_name" style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" contenteditable="true">{{ item.goods_name }}</td>
                                    <td style="width: 20%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="technical_parameters" contenteditable="{{ is_edit }}">{{ item.technical }}</td>
                                    <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="quantity" onblur="change_daxie(this)">{{ item.quantity }}</td>
                                    <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="unit_price" onblur="change_daxie(this)" contenteditable="{{ is_edit }}">{{ item.unit_price }}</td>
                                    <td style="width: 10%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="budget_amount">{{ item.budget_amount }}</td>
                                    <td style="width: 25%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: left;outline: none;">
                                        <span contentEditable="{{ is_edit }}" class="remarks" style="padding-left: 10px;outline: none;display: -moz-inline-box; display: inline-block;width:90%;white-space:pre-wrap;">{{ item.remarks }}</span>
                                        <span onclick="remove_time_adress(this)" style="color: red; cursor: pointer; display: inline-block;">删除</span>
                                    </td>
                                </tr>
                            {% endfor %}
                        {% else %}
                            <tr style="margin-left: 1%;">
                                <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="goods_name" contenteditable="{{ is_edit }}"></td>
                                <td style="width: 20%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="technical_parameters" contenteditable="{{ is_edit }}"></td>
                                <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="quantity" contenteditable="{{ is_edit }}"></td>
                                <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="unit_price" onblur="KeepTwoDecimal(this, this.innerText)" contenteditable="{{ is_edit }}"></td>
                                <td style="width: 10%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="budget_amount" onblur="change_daxie(this)" contenteditable="{{ is_edit }}"></td>
                                <td style="width: 25%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: left;outline: none;">
                                    <span contentEditable="{{ is_edit }}" class="remarks" style="padding-left: 10px;outline: none;display: -moz-inline-box; display: inline-block;width:100%;white-space:pre-wrap;"></span>
                                </td>
                            </tr>
                        {% endif %}
                    </table>
                    <table style="width: 100%;">
                        <tr>
                            <td colspan="4" style="border: #cfcfcf solid 0.5px;width: 95%;margin-left: 2.5%;height: 40px;padding-left: 0.5%;line-height: 40px;background: #F5F5F5;border-top: none;">
                                合计
                            </td>
                        </tr>
                        <tr style="width: 100%;">
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                总&nbsp;&nbsp;金&nbsp;额(元)
                            </td>
                            <td id="total_money" style="width: 35%;height: 37px;line-height: 37px;border: #cfcfcf solid 0.5px;">
                                {{ obj.total_money }}
                            </td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 37px;line-height: 37px;text-align: right;padding-right: 1%;">
                                大&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;写
                            </td>
                            <td style="width: 35%;height: 37px;line-height: 37px;border: #cfcfcf solid 0.5px;" id="daxie">
                                {{ obj.daxie }}
                            </td>
                        </tr>
                    </table>
                </div>
                <div style="width: 100%;">
                    <div style="width: 100%;height: 40px;line-height: 40px;background-color: #F5F5F5;color: #1E90FF;padding-left: 1%;border: #cccccc solid 1px;border-top: none;border-bottom: none;padding-right: 1%;">
                        上传附件
                        <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                            上传文件
                            <input type="file" name="party_main_body" id="party_main_body"
                                   onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
                        </a>
                    </div>
                    <table style="width: 100%;">
                        <thead>
                        <tr>
                            <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">序号</td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件名称
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                文件大小
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传时间
                            </td>
                            <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">
                                上传人员
                            </td>
                            <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">操作
                            </td>
                        </tr>
                        </thead>
                        <tbody style="overflow: auto;" id="party_main">
                            {% for record in file_list %}
                                <tr>
                                    <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">{{ forloop.counter }}</td>
                                    <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_name }}</td>
                                    <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.file_size }}</td>
                                    <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.upload_time }}</td>
                                    <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">{{ record.uploader }}</td>
                                    <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;"><a target='_blank' href="{{ record.file_path }}">查看</a> | <a style="color: red;" href='#' onclick='RemoveTr(this, "party_main", {{ record.id }})' >删除</a></td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script src="../../static/system_setup/creat_from/js/jquery-1.10.2.js"></script>
    <script>
        window.onload = function () {
            document.getElementById("loading").style.display = "none";
            document.getElementById("personage").className = "add_class";
            document.getElementById("sponsor").className = "add-nav-active";
        };
        const uuid = "{{ obj.fj_number | safe }}"
        // 上传附件
        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', uuid)
            form.append('source', 'zwkfk')     // 总务科付款
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        creat_table(JsonDate, tbody_id)
                    }
                }
            };
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            var local_host = window.location.host;
            if (Array.isArray(JsonData["pdf_path"])) {
                for (var i = 0; i < JsonData["pdf_path"].length; i++) {
                    var tr = document.createElement("tr");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                    td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                    td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";td5.innerHTML = JsonData["uploader"];
                    td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                    td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["pdf_path"][i]["id"] + "\")'>删除</a>";
                    td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                    tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1) {
                    document.getElementById("del_adjunct").style.display = 'block';
                }
            } else {
                tr = document.createElement("tr");td1 = document.createElement("td");td2 = document.createElement("td");td3 = document.createElement("td");td4 = document.createElement("td");td5 = document.createElement("td");td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length + 1;td1.style.textAlign = "center";td1.style.border = "#cccccc solid 1px";td1.style.borderTop = "none";
                td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border = "#cccccc solid 1px";td2.style.borderLeft = "none";td2.style.borderTop = "none";
                td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border = "#cccccc solid 1px";td3.style.borderLeft = "none";td3.style.borderTop = "none";
                td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border = "#cccccc solid 1px";td4.style.borderLeft = "none";td4.style.borderTop = "none";
                td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border = "#cccccc solid 1px";td5.style.borderLeft = "none";td5.style.borderTop = "none";
                td6.innerHTML = "<a target='_blank' href='" + JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \"" + tbody_id + "\", \"" + JsonData["id"] + "\")'>删除</a>";
                td6.style.textAlign = "center";td6.style.border = "#cccccc solid 1px";td6.style.borderLeft = "none";td6.style.borderTop = "none";
                tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i = 0; i < table.children.length; i++) {
                table.children[i].children[0].innerHTML = i + 1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id=" + String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }

        function save_data() {
            var form = new FormData();
            form.append("pay_reason", document.getElementById('reason_application').innerText);           // 申请事由
            form.append("rask", document.getElementById('rask').innerText);                                   // 备注
            form.append("total_money", document.getElementById('total_money').innerText);                         // 总金额
            form.append("daxie", document.getElementById('daxie').innerText);                         // 总金额
            form.append("fj_number", uuid);
            form.append("id", '{{ obj.id }}');
            form.append("revise", '{{ revise }}');
            form.append("people_type", document.getElementById('people_type').value)
            form.append("payee", document.getElementById('payee').innerText)
            form.append("account_number", document.getElementById('account_number').innerText)

            var goods_name_list = document.getElementsByClassName("goods_name");
            var technical_parameters = document.getElementsByClassName("technical_parameters");
            var quantity = document.getElementsByClassName("quantity");
            var unit_price = document.getElementsByClassName("unit_price");
            var budget_amount = document.getElementsByClassName("budget_amount");
            var remarks = document.getElementsByClassName("remarks");

            var apply_json = [];
            for (var i=0; i<goods_name_list.length; i++) {
                apply_json.push({"goods_name": goods_name_list[i].innerText, "technical": technical_parameters[i].innerText, "quantity": quantity[i].innerText,
                "unit_price": unit_price[i].innerText, "budget_amount": budget_amount[i].innerText, "remarks": remarks[i].innerText})
            }
            form.append("pay_detail", JSON.stringify(apply_json));                         // 付款明细

            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/finance/research_funding_darf.html", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert("保存成功！")
                            window.location.href = "research_funding_list.html";
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            };
        }

        function ExportCar() {
            var administrative_table = document.getElementById('party_main');
            if (!administrative_table.children.length) {
                alert("请上传附件！");
                return false
            }
            if (!reason_application) {
                alert("请填写申请事由！");
                return false
            }
            var people_type = document.getElementById("people_type").value
            if (people_type === "0") {
                alert("请选择人员类型！");
                return false
            }
            var form = new FormData();
            form.append("pay_reason", document.getElementById('reason_application').innerText);           // 申请事由
            form.append("rask", document.getElementById('rask').innerText);                                   // 备注
            form.append("total_money", document.getElementById('total_money').innerText);                         // 总金额
            form.append("daxie", document.getElementById('daxie').innerText);                         // 总金额
            form.append("fj_number", uuid);
            form.append("id", '{{ obj.id }}');
            form.append("people_type", people_type)
            form.append("payee", document.getElementById('payee').innerText)
            form.append("account_number", document.getElementById('account_number').innerText)

            var goods_name_list = document.getElementsByClassName("goods_name");
            var technical_parameters = document.getElementsByClassName("technical_parameters");
            var quantity = document.getElementsByClassName("quantity");
            var unit_price = document.getElementsByClassName("unit_price");
            var budget_amount = document.getElementsByClassName("budget_amount");
            var remarks = document.getElementsByClassName("remarks");

            var apply_json = [];
            for (var i=0; i<goods_name_list.length; i++) {
                apply_json.push({"goods_name": goods_name_list[i].innerText, "technical": technical_parameters[i].innerText, "quantity": quantity[i].innerText,
                "unit_price": unit_price[i].innerText, "budget_amount": budget_amount[i].innerText, "remarks": remarks[i].innerText})
            }
            form.append("pay_detail", JSON.stringify(apply_json));                         // 付款明细
            console.log(document.getElementById('total_money').innerText)
            document.getElementById("loading").style.display = "inline-block";
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/finance/research_funding_index.html", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200) {
                            alert("创建成功！")
                            window.location.href = "research_funding_list.html";
                        } else {
                            alert(JsonDate["message"])
                        }
                    }
                }
            };
        }

        function ToString(money) {
            console.log(money, '执行了吗');
            //汉字的数字
            var cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            //基本单位
            var cnIntRadice =['', '拾', '佰', '仟'];
            //对应整数部分扩展单位
            var cnIntUnits = ['', '万', '亿', '兆'];
            //对应小数部分单位
            var cnDecUnits = ['角', '分', '毫', '厘'];
            //整数金额时后面跟的字符
            var cnInteger = '整';
            //整型完以后的单位
            // var cnIntLast = '圆';
            var cnIntLast = '元';
            //最大处理的数字
            var maxNum = 999999999999999.9999;
            //金额整数部分
            var integerNum;
            //金额小数部分
            var decimalNum;
            //输出的中文金额字符串
            var chineseStr = '';
            //分离金额后用的数组，预定义
            var parts;
            if (money === '') { return ''; }
            money = parseFloat(money);
            if (money >= maxNum) {
                //超出最大处理数字
                return '';
            }
            if (money === 0) {
                chineseStr = cnNums[0] + cnIntLast + cnInteger;

                return chineseStr;
            }

            //转换为字符串
            money = money.toString();

            if (money.indexOf('.') === -1) {
                integerNum = money;
                decimalNum = '';
            } else {
                parts = money.split('.');
                integerNum = parts[0];

                decimalNum = parts[1].substring(0, 2);
            }
            //获取整型部分转换
            if (parseInt(integerNum, 10) > 0) {
                var zeroCount = 0;
                var IntLen = integerNum.length;
                for (var i = 0; i < IntLen; i++) {
                    // var n = integerNum.substr(i, 1);
                    var n = integerNum.substring(i, i+1);
                    var p = IntLen - i - 1;
                    var q = p / 4;
                    var m = p % 4;
                    if (n === '0') {
                        zeroCount++;
                    } else {
                        if (zeroCount > 0) {
                            chineseStr += cnNums[0];
                        }
                        //归零
                        zeroCount = 0;
                        chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                    }
                    if (m === 0 && zeroCount < 4) {
                        chineseStr += cnIntUnits[q];
                    }
                }
                chineseStr += cnIntLast;
            }
            //小数部分
            if (decimalNum !== '') {
                var decLen = decimalNum.length;
                for (var i = 0; i < decLen; i++) {
                    // var n = decimalNum.substr(i, 1);
                    var n = decimalNum.substring(i, i+1);
                    if (n !== '0') {
                        // chineseStr += cnNums[Number(n)] + cnDecUnits[i];
                        chineseStr += cnNums[parseInt(n)] + cnDecUnits[i];
                    }
                }
            }
            if (chineseStr === '') {
                chineseStr += cnNums[0] + cnIntLast + cnInteger;
            } else if (decimalNum === '') {
                chineseStr += cnInteger;
            }

            return chineseStr;
        }

        function change_daxie(objs) {
            var reg = new RegExp("[\\u4e00-\\u9FFF]+", "g")
            var unit_price_tag_list = document.getElementsByClassName('unit_price');        // 单价
            var quantity_tag_list = document.getElementsByClassName('quantity');       // 数量
            var budget_amount_list = document.getElementsByClassName('budget_amount');       // 预算金额

            if (reg.test(objs.innerText.replace(',', '').replace(',', '').replace(',', ''))) {
                alert("请输入数字!")
                return false
            }

            var total_number = 0
            for (var i=0; i<unit_price_tag_list.length; i++) {
                if (unit_price_tag_list[i].innerText && quantity_tag_list[i].innerText) {
                    budget_amount_list[i].innerText = formatMonenyNumber(parseFloat(unit_price_tag_list[i].innerText.replace(',', '').replace(',', '').replace(',', '').replace(',', '').replace(',', '')) * parseInt(quantity_tag_list[i].innerText));
                    total_number += parseFloat(budget_amount_list[i].innerText.replace(',', '').replace(',', '').replace(',', '').replace(',', '').replace(',', ''))
                }
            }
            if (objs.className === 'unit_price') {
                objs.innerText = formatMonenyNumber(objs.innerText.replace(',', '').replace(',', '').replace(',', '').replace(',', '').replace(',', ''));
            }
            document.getElementById("daxie").innerText=ToString(total_number);
            document.getElementById("total_money").innerText = formatMonenyNumber(total_number);
        }

        function KeepTwoDecimal(this_, number) {
            this_.innerText = formatMonenyNumber(String(number).replace(',', '').trim())
        }

        function formatMonenyNumber(number, toFixed = 2) {
            let str = [];
            if (!number) return 0
            if (typeof number === "number") number = String(number)
            let tempNumber = number.split('.');
            console.log(tempNumber)
            number = tempNumber[0]
            if (number.length <= 3) {
                if (tempNumber[1]) {
                    if (tempNumber[1].slice(0, toFixed).length < 2) {
                        number += '.' + tempNumber[1].slice(0, toFixed)+"0";
                    }else {
                        var decimils = tempNumber[1].slice(0, toFixed);
                if (decimils.length === 1){
                    decimils = decimils + "0";
                }
                number += '.' + decimils;
                    }
                }else {
                    number += '.00'
                }
                return number
            }
            number.split('').reverse().forEach((item, index) => {
                if (index !== 0 && index % 3 === 0) {
                    str.push(',', item)
                } else {
                    str.push(item)
                }
            })
            number = str.reverse().join('')
            if (tempNumber[1]) {
                if (tempNumber[1].slice(0, toFixed).length < 2) {
                    number += '.' + tempNumber[1].slice(0, toFixed)+"0";
                }else {
                    var decimils = tempNumber[1].slice(0, toFixed);
                if (decimils.length === 1){
                    decimils = decimils + "0";
                }
                number += '.' + decimils;
                }
            }else {
                 number += '.00'
            }
            return number;
        }

        function add_other() {
            var html = `<tr style="margin-left: 1%;">
                            <td class="goods_name" style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" contenteditable="true"></td>
                            <td style="width: 20%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="technical_parameters" contenteditable="true"></td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="quantity" onblur="change_daxie(this)" contenteditable="true"></td>
                            <td style="width: 15%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="unit_price" onblur="change_daxie(this)" contenteditable="true"></td>
                            <td style="width: 10%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: center;outline: none;" class="budget_amount"></td>
                            <td style="width: 25%;border: #cfcfcf solid 0.5px;height: 35px;line-height: 35px;text-align: left;outline: none;">
                                <span contentEditable="true" class="remarks" style="padding-left: 10px;outline: none;display: -moz-inline-box; display: inline-block;width:90%;white-space:pre-wrap;"></span>
                                <span onclick="remove_time_adress(this)" style="color: red; cursor: pointer; display: inline-block;">删除</span>
                            </td>
                        </tr>`
            $("#PaymentDetails").append(html);
        }

        function remove_time_adress(obj) {
            var trNode = $(obj).parent().parent();
            trNode.remove();

            var budget_amount_list = document.getElementsByClassName('budget_amount');       // 预算金额
            var total_number = 0
            for (var i=0; i<budget_amount_list.length; i++) {
                total_number += parseFloat(budget_amount_list[i].innerText.replace(',', '').replace(',', '').replace(',', ''))
            }

            document.getElementById("daxie").innerText=ToString(total_number);
            document.getElementById("total_money").innerText = formatMonenyNumber(total_number);
        }
    </script>
{% endblock %}
